/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.service;

import com.xlx.openLink.conf.ApplicationContextUtils;
import com.xlx.openLink.dao.PortMapper;
import com.xlx.openLink.entity.pojo.Port;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CacheManagerService {

    @Autowired
    private PortMapper portMapper;

    /**
     * @param pkGuid
     * @return
     */
    public List<Port> getPortList(String pkGuid) {
        EhCacheCacheManager ehCacheCacheManager = ApplicationContextUtils.applicationContext.getBean(EhCacheCacheManager.class);
        Cache cache = ehCacheCacheManager.getCache("portList");
        List<Port> ports = cache.get(pkGuid, List.class);
        if (ports == null || ports.size() == 0) {
            ports = portMapper.selectByDevId(pkGuid);
        }
        cache.put(pkGuid, ports);
        return ports;
    }

    /**
     * @param pkGuid
     * @param list
     */
    public void setPostList(String pkGuid, List<Port> list) {
        EhCacheCacheManager ehCacheCacheManager = ApplicationContextUtils.applicationContext.getBean(EhCacheCacheManager.class);
        Cache cache = ehCacheCacheManager.getCache("portList");
        cache.put(pkGuid, list);
    }

    public void clear() {
        EhCacheCacheManager ehCacheCacheManager = ApplicationContextUtils.applicationContext.getBean(EhCacheCacheManager.class);
        Cache cache = ehCacheCacheManager.getCache("portList");
        cache.clear();
    }
}
